package com.vietjetair.controller;

import com.vietjetair.common.CommUtil;
import com.vietjetair.common.RedisService;
import com.vietjetair.service.CheckTicketService;
import com.vietjetair.vo.TicketVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.awt.*;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;

/**
 * @author XiaoKun
 */
@Controller
public class CheckTicketController {

    @Resource
    private RedisService redisService;

    @Resource
    private CheckTicketService checkTicketService;

    private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");


    @RequestMapping("/")
    public String index() {
        //checkTicketService.test();
        return "index";
    }

    @RequestMapping("/check")
    public String check(TicketVo vo,Model model) throws InterruptedException, ParseException {
        long time = System.currentTimeMillis();
        out.println("-------------" + "开始查询,时间:"+format.format(new Date(time)) + "************");
        String ket = vo.getTxtDepartDateDay() + vo.getTxtReturnDateDay() + vo.getSelectOrigin() + vo.getSelectDestination();
        //if (!redisService.exists(ket)){
            vo.setKey(ket);
            Date date = format.parse("2019-" + vo.getTxtDepartDateMonth() + "-" + vo.getTxtDepartDateDay()+" 23:59:59");
            if (date.getTime()<time){
                out.println("不能为过去时间");
                return "redirect:/";
            }
            List<Map> ticketList = checkTicketService.priceChecking(vo);
            if (ticketList == null || ticketList.isEmpty()){
                return "redirect:/";
            }
            redisService.set(ket,ticketList,240L);
            redisService.set(ket+"1",vo,240L);
        //}
        List<Map> list = (List<Map>) redisService.get(ket);
        model.addAttribute("list",list);
        model.addAttribute("key",ket);
        model.addAttribute("trip",vo.getTrip());
        out.println("************" + "查询完毕,时间:"+format.format(new Date(System.currentTimeMillis()))+",总耗时:"+(System.currentTimeMillis()-time)/1000.0 + "************");
        return "list";
    }


    @RequestMapping("/chooseFlight")
    public Object chooseFlight(TicketVo vo) throws InterruptedException, IOException, AWTException {
        long time = 0;
        try {
            time = System.currentTimeMillis();
            out.println("-------------" + "开始扣位,时间:"+format.format(new Date(time)) + "************");
            List<Map> ticketMap = (List<Map>) redisService.get(vo.getKey());
            if (ticketMap==null){
                out.println("价格过期!");
                return "价格过期!";
            }
            TicketVo ticketVo = (TicketVo) redisService.get(vo.getKey()+"1");
            ticketVo.setGoId(vo.getGoId());
            ticketVo.setGoButton(vo.getGoButton());
            ticketVo.setComId(vo.getComId());
            ticketVo.setComButton(vo.getComButton());
            checkTicketService.fillInTheInformation(ticketVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "扣位完毕";
    }

    @ResponseBody
    @RequestMapping("/getUser")
    public String getUser(){
        List<String> emails = (List<String>) redisService.get("emails");
        if (emails != null && !emails.isEmpty()){
            StringBuilder sb = new StringBuilder();
            emails.forEach(s -> sb.append(s).append("\n"));
            return sb.toString();
        }else{
            return "哦豁,没有账号";
        }
    }
}
